﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using NUnit.Framework;
using DBSourceToolsLib;
using DBConnectionLib;

namespace TestDBSourceToolsLibNUnit
{
    //[TestFixture]
    public class TestDatabaseClass
    {
        

        [TestFixtureSetUp]
        public void Init()
        {
            FileHelper oFileHelper = new FileHelper();
            oFileHelper.DeleteFilesInDir(BaseDirectoryHelper.Unused + "\\output");
        }


        [Test]
        public void TestConnectionString()
        {
            
            Assert.AreEqual("Data Source=.;DataBase=AdventureWorksLT;Integrated Security=True;Connect Timeout=0;"
                , Constants.AdventureWorksLT_Database.ConnectionString, " Database connection string not correct");
        }
        [Test]
        public void TestCanConnect()
        {
            Assert.IsTrue(Constants.AdventureWorksLT_Database.TestSqlClientConnection(),
                Constants.AdventureWorksLT_Database.ErrorMessage);
            
        }
        [Test]
        public void TestThrowsFileDoesNotExist()
        {
            try
            {
                Constants.AdventureWorksLT_Database.RunScript("invalid_file_name");
                Assert.Fail(" did not throw Exception when file does not exist.");
            }
            catch (Exception ex)
            {
                string compareTarget = "File invalid_file_name does not exist.";
                string compareSource = ex.Message;
                Assert.IsTrue(compareTarget.CompareTo(compareSource) == 0, "Incorrect throw message");
            }
        }

        [Test]
        public void TestRunScript()
        {
            Database oDatabase = new Database(Constants.AdventureWorksLT_Database);
            using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(
                oDatabase.ConnectionString))
            {
                conn.Open();
                Assert.IsNotNull(conn, " could not connect to ");
                Assert.IsTrue(oDatabase.RunScript(
                    Constants.TestFiles_Control_ScriptRunner_Dir +
                    "select_script.sql"), " could not run " + Constants.TestFiles_Control_ScriptRunner_Dir + " select_script.sql");
                conn.Close();
            }

        }

        [Test]
        public void TestRunScriptDoesNotExist()
        {
            Database oDatabase = new Database(Constants.AdventureWorksLT_Database);
            using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(
                oDatabase.ConnectionString))
            {
                conn.Open();
                Assert.IsNotNull(conn, " could not connect to ");
                try
                {
                    string strInvalidScript = Constants.TestFiles_Control_ScriptRunner_Dir + "InvalidScript.sql";
                    Assert.True(System.IO.File.Exists(strInvalidScript), "File not found " + strInvalidScript);
                    oDatabase.RunScript(strInvalidScript);
                    Assert.Fail("Database did not throw an exception with invalid script");
                }
                catch (Exception ex)
                {

                    Assert.IsTrue(oDatabase.ErrorMessage.Contains("Msg"),
                        " incorrect error message : " + oDatabase.ErrorMessage);
                }

                conn.Close();
            }

        }

        [Test]
        public void TestRunInvalidScript()
        {
            Database oDatabase = new Database(Constants.AdventureWorksLT_Database);
            using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(
                oDatabase.ConnectionString))
            {
                conn.Open();
                Assert.IsNotNull(conn, " could not connect to ");
                try
                {
                    oDatabase.RunScript(Constants.TestFiles_Control_ScriptRunner_Dir + "InvalidScript.sql");
                    Assert.Fail("Database did not throw an exception with invalid script");
                }
                catch (Exception ex)
                {
                    Assert.IsTrue(oDatabase.ErrorMessage.Contains("Msg"),
                        " incorrect error message : " + oDatabase.ErrorMessage);
                }

                conn.Close();
            }

        }


        [Test]
        public void TestContainsSysObject()
        {
            Database oDatabase = new Database(Constants.AdventureWorksLT_Database);

            DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(
                new SourceDatabase(oDatabase, false)
                , oDatabase.BaseDirectory);
            Assert.IsNotNull(sysObjects, "SysObject in Database is null");
        }


        [Test]
        public void TestNewQueryName()
        {

            //Database oDatabase = new Database(Constants.AdventureWorksLT_Database);
            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(
            //    new SourceDatabase(oDatabase, false)
            //    , Constants.AdventureWorksLT_Database.BaseDirectory);
            //string strNewQueryName = sysObjects.NewQueryName();
            //Assert.IsTrue(strNewQueryName.CompareTo("Query1") == 0, "New Query should be called Query1");
        }

        [Test]
        public void TestNewQueryNameAfterLoad()
        {
            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(
            //    new SourceDatabase(Constants.AdventureWorksLT_Database, false)
            //    , Constants.AdventureWorksLT_BaseDirectory);
            //sysObjects.LoadFromDisk();
            ////string strNewQueryName = sysObjects.NewQueryName();
            //Assert.AreEqual("Query2", strNewQueryName);
        }

    }
}
